#include "seek_worker.h"
#include "libavformat/avformat.h"

void SeekWorker::Execute()
{
  SDL_LockMutex(mPlayer->mCtrlMutex);
  mPlayer->mSeeking = true;
  SDL_UnlockMutex(mPlayer->mCtrlMutex);
  SDL_LockMutex(mPlayer->mSeekMutex);
  int ret = av_seek_frame(mPlayer->mFmtCtx, mPlayer->mAudioIdx, mTime, AVSEEK_FLAG_FRAME);
  SDL_UnlockMutex(mPlayer->mSeekMutex);
  if (ret < 0)
  {
    SetErrorMessage("cannot seek");
    SDL_LockMutex(mPlayer->mCtrlMutex);
    mPlayer->mStopFlag = true;
    SDL_UnlockMutex(mPlayer->mCtrlMutex);
  }
  else
  {
    SDL_LockMutex(mPlayer->mCtrlMutex);
    mPlayer->mSeeking = false;
    mPlayer->mSeeked = true;
    mPlayer->mSeekedCnt = 0;
    SDL_UnlockMutex(mPlayer->mCtrlMutex);
  }
}

 void SeekWorker::Destroy(){
   mPlayer->UnrefPlayer();
   delete this;
 }